const http = require('http');
const clientReq = require('./client');

const api_dog = 'https://dog.ceo/api/breeds/image/random';
const api_cat = 'https://api.thecatapi.com/v1/images/search';

const server = http.createServer((req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('content-type', 'application/json');
  let api = '';
  if (req.url === '/api/cat-image') {
    api = api_cat;
  } else if (req.url === '/api/dog-image') {
    api = api_dog;
  } else {
    res.end('<h1>403 Forbidden<h1>');
    return;
  }
  clientReq(api).then(value => {
    res.statusCode = 200;
    res.end(value);
  }).catch(reason => {
    res.statusCode = 404;
    res.end(reason);
  });
});

server.listen(3000, () => {
  console.log('Start...');
});